package com.example.demo.zip.controller;

import com.example.demo.pdf.PdfDto;
import com.example.demo.zip.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zjq
 * @date 2022/1/13 - 17:01
 */
@RestController
@RequestMapping("/api/zip")
@Api(value = "zipcontroller", tags = {"zip生成"})
public class ZipController {
    //多文件pdf以压缩包形式下载
    @Autowired
    private ProductService productService;

    @ApiOperation(value = "ByteArrayOutputStream的形式创建zip文件（固定的地址，下在服务器上），不推荐使用")
    @PostMapping("createzip")
    public void downloadFai(HttpServletRequest req, HttpServletResponse response, @RequestBody List<PdfDto> dto) {
        productService.downLoadFile1(req, response, dto);
    }

    @ApiOperation(value = "先在服务器上生成临时文件，再压缩成zip文件（梧桐云能运行，这里未测试过）")
    @PostMapping("downpdfByZip")
    public void downpdfByZip(HttpServletRequest req, HttpServletResponse response, @RequestBody List<PdfDto> dto) {
        String filename = System.currentTimeMillis() + "";
        productService.zout(req, response, dto, filename);
    }


    @ApiOperation(value = "文件下载(所有格式，从服务器上下载)")
    @PostMapping("download")
    public void downloadFai(HttpServletRequest req, HttpServletResponse response) {
        String desPath = "I:\\java\\test.zip";
        productService.downLoadFile(req, response, desPath, "test.zip");
    }
}
